<?php 
class Admin extends Controller{
	function Admin(){
		parent::Controller();
	}

	function index(){	
		$data['id'] = $this->session->userdata('id');
		$gate['body'] = $this->load->view('admin/admin_home', $data, TRUE);
		$this->load->view('admin', $gate);
	}

	function student_list(){
		$data['id'] = $this->session->userdata('id');
		$students = $this->Moderator->getlist("students");
		$data['students'] = $students;

		$pending = $this->Moderator->getPending('students', 'regstatus', 'On Trial');
		$data['pending'] = $pending;

		$gate['body'] = $this->load->view('admin/student_list', $data, TRUE);
		$this->load->view('admin', $gate);
	}

	function update_student_status(){
		$data['pending'] = $this->Moderator->getPending('students', 'regstatus', 'On Trial');			
		$i = 1;
		foreach($data['pending']->result() AS $list){
			$list->regstatus = $this->input->post('regstatus'.$i);
			$this->Moderator->updateStatus('students', $list, $list->email);
			echo 'regstatus'.$i;
			$i++;
		} redirect('admin/student_list');
	}

	function teacher_list(){
		$data['id'] = $this->session->userdata('id');
		$teachers = $this->Moderator->getlist("teachers");
		$data['teachers'] = $teachers;

		$pending = $this->Moderator->getPending('teachers', 'teacherstatus', 'pending');
		$data['pending'] = $pending;

		$gate['body'] = $this->load->view('admin/teacher_list', $data, TRUE);
		$this->load->view('admin', $gate);
	}

	function update_teacher_status(){
		$data['pending'] = $this->Moderator->getPending('teachers', 'teacherstatus', 'pending');			
		$i = 1;
		foreach($data['pending']->result() AS $list){
			$list->teacherstatus = $this->input->post('teacherstatus'.$i);
			$this->Moderator->updateStatus('teachers', $list, $list->email);
			$i++;
		} redirect('admin/teacher_list');
	}


	function manage_class(){
		$data = array();
		$data['id'] = $this->session->userdata('id');

		$rules = array();
		$rules['subjectname'] = "required";
		$rules['description'] = "required";

		$this->validation->set_rules($rules);
		$this->validation->set_error_delimiters('<div id="error">', '</div>');
		
		$fields = array();
		$fields['subjectname'] = "Subject Name";
		$fields['description'] = "Description";

		$this->validation->set_fields($fields);
		
		if($this->validation->run()==TRUE){
			$new['subjectname'] = $this->input->post('subjectname');
			$new['description'] = $this->input->post('description');

			$this->Moderator->insertClass('subjects', $new);			
			redirect('admin/manage_class');
		}

		$classes = $this->Moderator->getClasses('subjects');
		$data['classes'] = $classes;

		$gate['body'] = $this->load->view('admin/classes', $data, TRUE);
		$this->load->view('admin', $gate);
	}

	function delete_class(){
		$toDelete = $this->input->post('class');
		$this->Moderator->deleteClass($toDelete);
		redirect('admin/manage_class');
	}

	function manage_sched(){
		$data['id'] = $this->session->userdata('id');
		$viewAll = $this->Moderator->viewAllSchedules();
		$data['viewAll'] = $viewAll;
		
		$sched = $this->Moderator->getSchedules();
		$data['sched'] = $sched;

		$gate['body'] = $this->load->view('admin/schedules', $data, TRUE);
		$this->load->view('admin', $gate);
	}

	function update_schedule_status(){
		$data['pending'] = $this->Moderator->getPending('schedule', 'adminstatus', 'pending');			
		$i = 1;
		foreach($data['pending']->result() AS $list){
			$list->adminstatus = $this->input->post('adminstatus'.$i);
			$this->Moderator->updateSchedule('schedule', $list, $list->teacherid, $list->date, $list->time);
			$i++;
		} redirect('admin/manage_sched');
	}

	function announcement(){
		$data = array();
		$data['id'] = $this->session->userdata('id');

		$rules = array();
		$rules['title'] = "required";
		$rules['message'] = "required";

		$this->validation->set_rules($rules);
		$this->validation->set_error_delimiters('<div id="error">', '</div>');
		
		$fields = array();
		$fields['title'] = "Title";
		$fields['message'] = "Announcement";

		$this->validation->set_fields($fields);

		if($this->validation->run()==TRUE){
		echo "HELLO";
			$new['title'] = $this->input->post('title');
			$new['message'] = $this->input->post('message');
			$new['for'] = $this->input->post('for');
			
			$this->Moderator->addAnnouncement('announcements', $new);
			
		}

		$classes = $this->Moderator->getClasses('subjects');
		$data['classes'] = $classes;

		$gate['body'] = $this->load->view('admin/announcement', $data, TRUE);
		$this->load->view('admin', $gate);
	}
}
?>
